from django.contrib.auth.models import User
from django.db import models

# Create your models here.
from utils.modelsMiXin import ModelSetMiXin
from work.models import Label


class Article(ModelSetMiXin): #文章
    STATUS_CHOICES = (
        (0,"未发布"),
        (1,"已发布")
    )
    title = models.CharField(max_length=100,verbose_name="标题")
    digest = models.CharField(max_length=300,verbose_name="摘要")
    content = models.TextField(verbose_name="文章内容") #文章内容直接用Text，没有字数限制
    page_view = models.IntegerField(verbose_name="浏览量",default=0) #默认default为0
    priority = models.IntegerField(verbose_name="优先级",default=0)
    status = models.IntegerField(verbose_name="状态",default=0,choices=STATUS_CHOICES)

    label = models.ForeignKey(Label,on_delete=models.CASCADE)
    user = models.ForeignKey(User,on_delete=models.CASCADE)

    class Meta:
        ordering = ['-priority','-page_view','-create_time']#搜索后排序，首先按照优先级从高到低排序，然后才是浏览量
        db_table = "article" #数据表名
        verbose_name = "文章"
        verbose_name_plural = verbose_name

    def __str__(self):  #当别人搜索后，返回给他们的是什么
        return f'{self.id},{self.title}'

class Comment(ModelSetMiXin): #评论
    content = models.TextField(verbose_name="评论内容")
    level = models.IntegerField(verbose_name="评论等级",default=1)
    parent_comment = models.IntegerField(verbose_name="父级评论",null=True)
    reply_comment = models.IntegerField(verbose_name="回复评论",null=True)

    article = models.ForeignKey(Article,on_delete=models.CASCADE)
    user = models.ForeignKey(User,on_delete=models.CASCADE)

    class Meta:
        ordering = ['-create_time']
        db_table = "comment"
        verbose_name = "评论"
        verbose_name_plural = verbose_name
